package spotGUI.optionsGUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import managers.FastProperties;
import managers.OptionsManager;
import net.tomahawk.XFileDialog;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.SwingConstants;

public class MessengerPanel extends JPanel {
	private JPanel windowsOptions;
	private JCheckBox chckbxFloatingConversationsOnTop;
	private JCheckBox chckbxAlertBeforeClosingFloatingConversation;
	private JPanel conversationOptions;
	private JCheckBox chckbxViewTimeOnMessages;
	private JCheckBox chckbxViewDateOnMessages;
	private JCheckBox chckbxKKiller;
	private JCheckBox chckbxStoreConversations;
	private JButton btbBrowse;
	private JTextArea conversationFolder;

	private FastProperties options;
	private XFileDialog fileChooser;
	private JComboBox comboBox;
	private JLabel lblLingua;

	public MessengerPanel() {

		options = OptionsManager.getSharedInstance().getOptionGroup("messenger");
		fileChooser = new XFileDialog("Sfoglia");

		ItemListener checkBoxListener= new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == 1) {
					options.setProperty(((JCheckBox)e.getItem()).getName(), "true");
				} else {
					options.setProperty(((JCheckBox)e.getItem()).getName(), "false");
				}
				options.store();
			}
		};
		setLayout(null);

		windowsOptions = new JPanel();
		windowsOptions.setBounds(10, 120, 619, 56);
		windowsOptions.setBorder(new TitledBorder(null, ResourceBundle.getBundle("spotGUI.optionsGUI.messages").
				getString("MessengerPanel.windowsOptions.text"), TitledBorder.LEFT, TitledBorder.TOP, null, new Color(59, 59, 59)));
		add(windowsOptions);
		SpringLayout sl_windowsOptions = new SpringLayout();
		windowsOptions.setLayout(sl_windowsOptions);



		chckbxFloatingConversationsOnTop = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxFloatingConversationsOnTop.text")); //$NON-NLS-1$ //$NON-NLS-2$
		chckbxFloatingConversationsOnTop.addItemListener(checkBoxListener);
		chckbxFloatingConversationsOnTop.setName("floatingConversationsOnTop");
		sl_windowsOptions.putConstraint(SpringLayout.NORTH, chckbxFloatingConversationsOnTop, 0, SpringLayout.NORTH, windowsOptions);
		sl_windowsOptions.putConstraint(SpringLayout.WEST, chckbxFloatingConversationsOnTop, 10, SpringLayout.WEST, windowsOptions);
		if (options.getProperty(chckbxFloatingConversationsOnTop.getName()).equals("true")) {
			chckbxFloatingConversationsOnTop.setSelected(true);
		} else {
			chckbxFloatingConversationsOnTop.setSelected(false);
		}
		windowsOptions.add(chckbxFloatingConversationsOnTop);

		chckbxAlertBeforeClosingFloatingConversation = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxAlertBeforeClosingFloatingConversation.text")); //$NON-NLS-1$ //$NON-NLS-2$
		sl_windowsOptions.putConstraint(SpringLayout.NORTH, chckbxAlertBeforeClosingFloatingConversation, 0, SpringLayout.NORTH, chckbxFloatingConversationsOnTop);
		sl_windowsOptions.putConstraint(SpringLayout.EAST, chckbxAlertBeforeClosingFloatingConversation, -10, SpringLayout.EAST, windowsOptions);
		chckbxAlertBeforeClosingFloatingConversation.addItemListener(checkBoxListener);
		chckbxAlertBeforeClosingFloatingConversation.setName("alertBeforeClosingFloatingConversation");
		if (options.getProperty(chckbxAlertBeforeClosingFloatingConversation.getName()).equals("true")) {
			chckbxAlertBeforeClosingFloatingConversation.setSelected(true);
		} else {
			chckbxAlertBeforeClosingFloatingConversation.setSelected(false);
		}
		windowsOptions.add(chckbxAlertBeforeClosingFloatingConversation);

		conversationOptions = new JPanel();
		conversationOptions.setBounds(10, 4, 619, 112);
		conversationOptions.setBorder(new TitledBorder(null, ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.conversationOptions.text")
				, TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(conversationOptions);
		SpringLayout sl_conversationOptions = new SpringLayout();
		conversationOptions.setLayout(sl_conversationOptions);

		chckbxViewTimeOnMessages = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxViewTimeOnMessages.text")); //$NON-NLS-1$ //$NON-NLS-2$
		chckbxViewTimeOnMessages.addItemListener(checkBoxListener);
		chckbxViewTimeOnMessages.setName("viewTimeOnMessages");
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, chckbxViewTimeOnMessages, 0, SpringLayout.NORTH, conversationOptions);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, chckbxViewTimeOnMessages, 0, SpringLayout.WEST, conversationOptions);
		if (options.getProperty(chckbxViewTimeOnMessages.getName()).equals("true")) {
			chckbxViewTimeOnMessages.setSelected(true);
		} else {
			chckbxViewTimeOnMessages.setSelected(false);
		}
		conversationOptions.add(chckbxViewTimeOnMessages);

		chckbxViewDateOnMessages = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxViewDateOnMessages.text")); //$NON-NLS-1$ //$NON-NLS-2$
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, chckbxViewDateOnMessages, 0, SpringLayout.NORTH, chckbxViewTimeOnMessages);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, chckbxViewDateOnMessages, 34, SpringLayout.EAST, chckbxViewTimeOnMessages);
		chckbxViewDateOnMessages.addItemListener(checkBoxListener);
		chckbxViewDateOnMessages.setName("viewDateOnMessages");
		if (options.getProperty(chckbxViewDateOnMessages.getName()).equals("true")) {
			chckbxViewDateOnMessages.setSelected(true);
		} else {
			chckbxViewDateOnMessages.setSelected(false);
		}
		conversationOptions.add(chckbxViewDateOnMessages);

		chckbxKKiller = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxKKiller.text"));
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, chckbxKKiller, 6, SpringLayout.SOUTH, chckbxViewDateOnMessages);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, chckbxKKiller, 0, SpringLayout.WEST, chckbxViewDateOnMessages);
		chckbxKKiller.addItemListener(checkBoxListener);
		chckbxKKiller.setName("kAlert");
		if (options.getProperty(chckbxKKiller.getName()).equals("true")) {
			chckbxKKiller.setSelected(true);
		} else {
			chckbxKKiller.setSelected(false);
		}
		conversationOptions.add(chckbxKKiller);

		chckbxStoreConversations = new JCheckBox(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.chckbxStoreConversations.text"));
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, chckbxStoreConversations, 6, SpringLayout.SOUTH, chckbxViewTimeOnMessages);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, chckbxStoreConversations, 0, SpringLayout.WEST, chckbxViewTimeOnMessages);
		chckbxStoreConversations.addItemListener(checkBoxListener);
		chckbxStoreConversations.setName("storeConversations");
		if (options.getProperty(chckbxStoreConversations.getName()).equals("true")) {
			chckbxStoreConversations.setSelected(true);
		} else {
			chckbxStoreConversations.setSelected(false);
		}
		conversationOptions.add(chckbxStoreConversations);

		btbBrowse = new JButton(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.btbBrowse.text"));
		btbBrowse.setToolTipText(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.btbBrowse.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, btbBrowse, 6, SpringLayout.SOUTH, chckbxStoreConversations);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, btbBrowse, 28, SpringLayout.WEST, conversationOptions);
		btbBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String dir = fileChooser.getFolder();
				if (dir != null) {
					conversationFolder.setText(dir);
					options.setProperty(conversationFolder.getName(), dir);
					options.store();
				}
			}
		});
		conversationOptions.add(btbBrowse);

		conversationFolder = new JTextArea();
		sl_conversationOptions.putConstraint(SpringLayout.NORTH, conversationFolder, 4, SpringLayout.NORTH, btbBrowse);
		sl_conversationOptions.putConstraint(SpringLayout.WEST, conversationFolder, 14, SpringLayout.EAST, btbBrowse);
		sl_conversationOptions.putConstraint(SpringLayout.EAST, conversationFolder, -10, SpringLayout.EAST, conversationOptions);
		conversationOptions.add(conversationFolder);
		conversationFolder.setOpaque(false);
		conversationFolder.setEditable(false);
		conversationFolder.setName("conversationFolder");
		conversationFolder.setText(options.getProperty(conversationFolder.getName()));
		
		comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.SELECTED) {
					Locale selected = (Locale) ((JComboBox)arg0.getSource()).getSelectedItem();
					OptionsManager.getSharedInstance().getOptionGroup("general").setProperty("locale", selected.toString());
					OptionsManager.getSharedInstance().storeOptionGroup("general");
				}
			};
		});
		comboBox.setRenderer(new ListCellRenderer() {
			
			ListCellRenderer defaultRenderer = comboBox.getRenderer();
			@Override
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
				Locale locale = (Locale) value;
				JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
				label.setText(locale.getDisplayLanguage());
				return label;
			}
		});
		comboBox.setBackground(UIManager.getColor("Button.background"));
		Locale[] locales = {Locale.ITALIAN, Locale.ENGLISH};
		comboBox.setModel(new DefaultComboBoxModel(locales));
		comboBox.setBounds(92, 185, 114, 22);
		comboBox.setSelectedItem(new Locale(OptionsManager.getSharedInstance().getOptionGroup("general").getProperty("locale")));
		add(comboBox);
		
		lblLingua = new JLabel(ResourceBundle.getBundle("spotGUI.optionsGUI.messages").getString("MessengerPanel.lblLingua.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblLingua.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLingua.setBounds(10, 188, 78, 16);
		add(lblLingua);

	}		
}
